#include"system.h"
node* init(void)
{
    node*p=(node*)calloc(1,sizeof(node));
    if(p==NULL)
        printf("失败");
    p->next=p;
    p->prev=p;
    return p;
}
node1* init1_1(void)
{
    node1* p;
    p=(node1*)calloc(1,sizeof(node1));
    if(p==NULL)
        printf("失败");
    strcpy(p->name,"CLF");
    strcpy(p->number,"1234");
    p->next=NULL;
    return p;
}
list* init1(void)
{
    list*p=(list*)calloc(1,sizeof(list));
    if(p==NULL)
        printf("list失败");
    p->next=p;
    p->prev=p;
    return p;
}
void inset(node*head,node*new)
{
    new->next=head->next;
    new->prev=head;
    head->next->prev=new;
    head->next=new;
}
void inset1(list*head,list*new)
{
    new->next=head->next;
    new->prev=head;
    head->next->prev=new;
    head->next=new;
}
void delete(node*head)
{
    head->prev->next=head->next;
    head->next->prev=head->prev;
    head->prev=head;
    head->next=head;
    free(head);
}
void delete_node(node*s)
{
    node*o=s->next;
    while(o!=s)
    {
        delete1(o);
        free(o);
        o=o->next;
    }
}
void delete1(list*head)
{
    head->prev->next=head->next;
    head->next->prev=head->prev;
    head->prev=head;
    head->next=head;
    free(head);
}
void delete_list(list*s)
{
    list*o=s->next;
    while(o!=s)
    {
        delete1(o);
        free(o);
        o=o->next;
    }
}
node* check(char *p1,char*p2,node*m,char a)
{
    node* p=m;
    int i=strlen(p1),j,h=strlen(p2),h1,l;
    do
    {
        j=strlen(p->data1),h1=strlen(p->data2);
        if(p->i==a)
        {
            l=0;
            for (uint8_t i1=0;i==j&&i1<j;i1++)
                if(*p1++!=*(p->data1+i1))
                    l++;
            for (uint8_t i2=0;h==h1&&i2<h1;i2++)
                if(*p2++!=*(p->data2+i2))
                    l++;
            if(l==0)
                return p;
        }
        p=p->next;
    }while(p!=m);
    return NULL ;
}
node*check1(node*head,char *i,char a)//没有差值就返回节点
{
    node*p=head;
    do
    { 
        if(!strcmp(p->data1,i)&&p->i==a)
            return p;
        p=p->next;
    }while(p!=head);
    return NULL;
}
node* Registered()
{
    node*new=init();
    char a,l;
    printf("请输入账号\n");
    scanf("%s",new->data1);
    while(getchar()!='\n');
    printf("请输入密码\n");
    scanf("%s",new->data2);
    while(getchar()!='\n');
    while(1)
    {
        printf("请输入账号类型(教师端:t,学生端:s)\n");
        scanf("%c",&a);
        while(getchar()!='\n');
        if(a=='t')
        {
            new->i=a;
            break;
        }
        else if(a=='s')
            if(strlen(new->data1)==numbermax-1)
            {
                new->i=a;
                break;
            }
            else
            {
                printf("输入账号位数不对是否退出(y/n)\n");
                scanf("%c",&l);
                while(getchar()!='\n');
                if(l=='y')
                    break;
            }
        else
            printf("输入类型错误\n");
    }
    return new;
}
list*find_stu(list*head,char *i,char j)//返回相等的
{
    list*p=head;
    do
    { 
        if(j=='y')
            if(!strcmp(p->name,i))
                return p;
        else if(j=='n')
            if(!strcmp(p->number,i))
                return p;
        p=p->next;
    }while(p!=head);
    return NULL;
}
node*find_stu1(node*head,char *i)//返回相等的
{
    node*p=head;
    do
    { 
        if(!strcmp(p->data1,i))
            return p;
        p=p->next;
    }while(p!=head);
    return NULL;
}
int show4(node*head)
{
    node*p=head;
    int a=0,j=0;
    do
    {
        if(p->i=='s')
        printf("学号：%s\n",p->data1),j++;
        p=p->next;
        a++;
    }while(p!=head);
    if(a==0)
        printf("没有学号,需管理员申请\n");
    return j;
}
void input(list*head,node*t)
{
    
    char i,j='y',s;
    int y;
    printf("------------------------------------\n");
    y=show4(t);
    printf("学号需要于账号相同,否则去与管理员申请\n");
    printf("-------------------------------------\n");
    for(int r=0;r<y;r++)
    {
        list*n=init1();
        char *q=n->name;
        char *p=n->number;
        do
        {
            printf("请输入名称:\n");
            scanf("%s",q);
            while(getchar()!='\n');
            if(find_stu(head,q,'y')!=NULL)
            {
                printf("名字重复\n");
                printf("是否重名(y/n)\n");
                scanf("%c",&i);
                while(getchar()!='\n');
                if(i=='y')
                {
                    strcpy(head->name,q);
                    break;
                }
                else if(i=='n')
                {
                    printf("是否退出(y/n)\n");
                    scanf("%c",&j);
                    while(getchar()!='\n');
                    if(j=='y')
                        return;
                }
            }
            else
                j='y';
        }while(j=='n');
        printf("请输入性别:\n");
        scanf("%s",n->gender);
        while(getchar()!='\n');
        while(j=='y')
        {
            printf("请输入学号:\n");
            scanf("%s",p);
            while(getchar()!='\n');
            if(find_stu1(t,p)==NULL)
                printf("学号与账号有偏差");
            else
                if(find_stu(head,p,'n')!=NULL)
                    printf("学号重复\n");
                else
                    break;
        }
        printf("请输入高数成绩:\n");
        scanf("%f",&n->data1);
        while(getchar()!='\n');
        printf("请输入C语言成绩:\n");
        scanf("%f",&n->data2);
        while(getchar()!='\n');
        inset1(head,n);
    }
}
void modify(node*s)
{
    char data1[max],data2[max];
    printf("请修改账号:");
    scanf("%s",data1);
    while(getchar()!='\n');
    strcpy(s->data1,data1);
    printf("请修改密码:");
    scanf("%s",data2);
    while(getchar()!='\n');
    strcpy(s->data2,data2);
}
void Pass_modify(node*s)
{
    char data[max];
    printf("请输入密码:");
    scanf("%s",data);
    while(getchar()!='\n');
    strcpy(s->data2,data);
}
void stu_input(list*s)
{
    printf("名字:%s,性别:%s,学号:%s,高数成绩:%f,C语言成绩:%f\n",s->name,s->gender,s->number,s->data1,s->data2);
    printf("请输入高数成绩\n");
    scanf("%f",s->data1);
    while(getchar()!='\n');
    printf("请输入C语言成绩\n");
    scanf("%f",s->data2);
    while(getchar()!='\n');
}
node* celect(node*head,char *i)
{
    node*p=head->next,*o[3];
    int a=0;
    char c;
    while(p!=head)
    { 
        if(!strcmp(p->data1,i))
        {
            printf("账号：%s,密码：%s.",p->data1,p->data2);
            if(p->i=='s')
                printf("账号类型:学生端\n"),o[0]=p;
            if(p->i=='t')
                printf("账号类型:教师端\n"),o[1]=p;
            o[2]=p;
            a++;
        }
        p=p->next;
    }
    while(a==2)
    {
        printf("请选择账号类型(教师端:t/学生端:s)");
        scanf("%c",&c);
        while(getchar()!='\n');
        if(c=='s')
            return o[0];
        else if(c=='t')
            return o[1];
        else
            printf("输入错误重新输入");
    }
    return o[2];
}
void operate (node*head)
{
    char a[max];
    printf("请输入账号:");
    scanf("%s",a);
    while(getchar()!='\n');
    node*c=celect(head,a);
    char l;
    if(c!=0)
        while(1)
        {
            printf("请输入需要执行的操作:(a:删除账号,b:修改账号,q:退出)\n");
            scanf("%c",&l);
            while(getchar()!='\n');
                switch(l)
                {
                    case 'a':
                        delete(c);
                        break;
                    case 'b':
                        modify(c);
                        break;
                }
                if(l=='q')
                    break;
        }
    else
        printf("没有该账号\n");
}
void operate1 (list*head)
{
    char a[max];
    char i=0,j;
    list*c;
    while (i==0)
    {
        do
        {
            printf("请选择输入名字或学号(y:名字/n:学号):\n");
            scanf("%c",&i);
            while(getchar()!='\n');
            if(i=='y'||i=='n')
                break;
            else
                printf("输入错误\n");
        }while(1);
        printf("请输入:\n");
        scanf("%s",a);
        while(getchar()!='\n');
        if((c=find_stu(head,a,i))==NULL)
        {
            printf("未检索到该信息:\n");
            printf("是/否继续输入(y/n)\n");
            scanf("%c",&j);
            while(getchar()!='\n');
            if(j=='n')
                break;
            i=0;
        }
        else  
            do
            {      
                printf("姓名：%s,性别：%s,学号:%s,高数成绩:%g,C语言成绩:%g.\n",c->name,c->gender,c->number,c->data1,c->data2);
            } 
            while ((c=find_stu(c,a,i))!=NULL);
    }
    if(c!=NULL)
    {
        while(1)
        {
            printf("/********************************/\n");
            printf(" a:成绩录入\n");
            printf(" b:成绩修改\n");
            printf(" c:信息删除\n");
            printf(" q:退出\n");
            printf("/********************************/\n");
            printf("请输入需要执行的操作:\n");
            scanf("%c",&j);
            while(getchar()!='\n');
                switch(j)
                {
                    case 'a':
                    case 'b':
                        stu_input(c);
                        break;
                    case 'c':
                        delete1(c);
                        break;
                    case 'q':
                        return;
                    default:
                        printf("输入错误");
                }
        }
    }
    else
        printf("没有该账号\n");
}

void show2(list*head)
{
    list*p=head->next;
    int a=0;
    while(p!=head)
    {
        printf("姓名：%s,性别：%s,学号:%s,高数成绩:%f,C语言成绩:%f.\n",p->name,p->gender,p->number,p->data1,p->data2);
        p=p->next;
        a++;
    }
    if(a==0)
        printf("没有信息\n"); 
}
void show3(list*head,node*new)
{
    list*c=find_stu(head,new->data1,'n');
    printf("高数成绩:%f,C语言成绩:%f.\n",c->data1,c->data2);
}
void show1(node*head)
{
    node*p=head;
    int a=0;
    do
    {
        printf("账号:%s,密码:%s,",p->data1,p->data2);
        p->i=='s'?printf("学生端\n"):p->i=='t'?printf("教师端\n"):0;
        p=p->next;
        a++;
    }while(p!=head);
    if(a==0)
        printf("没有账号\n"); 
}
void pint2()
{
    printf("/********************************/\n");
    printf("a:显示所有账号信息\n");
    printf("b:查询单个账号信息(并对其进行操作)\n");
    printf("c:增加账号(*注意学生端账号就是学号应当满足%d位!!!!!)\n",numbermax-1);
    printf("q:退出\n");
    printf("/*******************************/\n");
    printf("请输入:\n");
}
void administrator_Function(node1*s)
{
    node *o;
    char i;
    static int l=0;
    printf("/*******************************/\n");
    printf("管理员:%s,工号：%s\n",s->name,s->number);
    printf("欢迎使用本系统？\n");
    printf("/*******************************/\n");
    while(1)
    {
        pint2();
        scanf("%c",&i);
        while(getchar()!='\n');
            switch(i)
            {
                case 'a':
                    if(l>0)
                        show1(s->next);
                    break;
                case 'b':
                    if(l>0) 
                        operate(s->next);
                    break;
                case 'c':
                    o=Registered();
                    if(l>0)
                        if(check1(s->next,o->data1,o->i)==NULL)
                            inset(s->next,o);
                        else
                            printf("账号重复请重新输入\n");
                    else
                        s->next=o,l=1;
                    break;
                case 'q':
                    return;
                default:
                    printf("输入错误:\n");
            }
    }
}
void exchange(list*s)
{
    s->next->prev=s->prev;
    s->prev->next=s->next;
    s->prev=s->next;
    s->next=s->next->next;
    s->prev->next=s;
    s->next->prev=s;
}
void ranking(list*s)
{
    list*p=s;
    int a=0;
    char u1,u2;
    printf("/********************************/\n");
    printf(" a:按高数成绩\n");
    printf(" b:按C语言成绩\n");
    printf("/********************************/\n");
    printf("请输入:\n");
    scanf("%c",&u1);
    while(getchar()!='\n');
    printf("/********************************/\n");
    printf(" a:升序\n");
    printf(" b:倒序\n");
    printf("/********************************/\n");
    printf("请输入:\n");
    scanf("%c",&u2);
    while(getchar()!='\n');
    printf("%c",u2);
    printf("%c",u1);
    do
    {
        a=0;
        while(p->next->next!=s)
        {
            u1=='a'?p->next->data1<p->next->next->data1&&u2=='b'?exchange(p->next)\
            ,a++:p->next->data1>p->next->next->data1&&u2=='a'?exchange(p->next)\
            ,a++:0:u1=='b'?p->next->data2<p->next->next->data2&&u2=='b'?\
            exchange(p->next),a++:p->next->data2>p->next->next->data2&&u2=='a'?exchange(p->next),a++:0:0;
            p=p->next;
        }
        p=s;
    }while(a!=0);
}
void pint1(char o)
{
    if(o=='t')
    {
        printf("/********************************/\n");
        printf(" a:显示所有学生信息\n");
        printf(" b:查询单个学生信息(并对其进行操作)\n");
        printf(" c:增加学生信息\n");
        printf(" d:学生成绩排序\n");
        printf(" e:修改密码\n");
        printf(" q:退出\n");
        printf("/********************************/\n");
    }
    else if(o=='s')
    {
        printf("/********************************/\n");
        printf(" a:查询成绩\n");
        printf(" e:修改密码\n");
        printf(" q:退出\n");
        printf("/********************************/\n");
    }
}
void teacherORstudent_working(list*s,node*t) 
{
    char i;
    while(1)
    {
        pint1(t->i);
        while(1)
        {
            printf("请输入:\n");
            scanf("%c",&i);
            while(getchar()!='\n');
            if(t->i=='s')
            {
                if(!(i=='b'||i=='c'||i=='d'||i=='q'))
                    printf("输入错误,请重新输入\n");
            }
            else
                break;
        }
        switch(i)
        {
            case 'a':
                t->i=='t'?show2(s):show3(s,t);
                break;
            case 'b':
                operate1(s);
                break;
            case 'c':
                input(s,t);
                break;
            case 'd':
                ranking(s);
                break;
            case 'e':
                Pass_modify(t);
                break;
            case 'q':
                return;
            default:
                printf("输入错误:\n");
        }
    }
}
void teacherORstudent_Function(node*s,list*t,char o)
{
    char a[max];
    char b[max];
    char l,j;
    node*p;
    o=='t'?printf("是/否进入教师端(y/n)\n"):o=='s'? printf("是/否进入学生端(y/n)\n"):0;
    scanf("%c",&l);
    while(getchar()!='\n');
    while(1)
    {
        if(l=='y')
        {
            printf("请输入账号\n");
            scanf("%s",a);
            while(getchar()!='\n');
            printf("请输入密码\n");
            scanf("%s",b);
            while(getchar()!='\n');
            if((p=check(a,b,s,o))==NULL)
            {
                printf("账号或者密码错误\n");
                printf("是/否继续输入(y/n)\n");
                scanf("%c",&j);
                while(getchar()!='\n');
                if(j=='n')
                    break;
            }
            else 
            {
                teacherORstudent_working(t,p);
                break;
            }
        }
        else if(l=='n')
            break;
        else
            printf("输入错误\n");
    }
}

void MAIN(node1*head,list*l)
{
    char i;
    while(1)
    {
        printf("/***********/\n");
        printf(" a:管理员\n");
        printf(" b:学生端\n");
        printf(" c:教师端\n");
        printf(" q:退出系统\n");
        printf("/***********/\n");
        printf("请输入:\n");
        scanf("%c",&i);
        while(getchar()!='\n');
        switch(i)
        {
            case 'a':
                administrator_Function(head);
                break;
            case 'b':
                teacherORstudent_Function(head->next,l,'s');
                break;
            case 'c':
                teacherORstudent_Function(head->next,l,'t');
                break;
            case 'q':
                delete_list(l);
                delete_node(head->next);
                free(head->next);
                free(l);
                return;
            default:
                printf("输入错误\n");
        }   
    }
}